package com.example.boot3.controller;


import com.alibaba.excel.EasyExcel;
import com.example.boot3.model.Driver;
import com.example.boot3.model.Recharge;
import com.example.boot3.model.Refuel;
import com.example.boot3.service.DriverService;
import com.example.boot3.util.JsonData;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@CrossOrigin(origins =  "*")
@RestController
//@RequestMapping("/api/driver")
@RequestMapping("/driver")
public class DriverController {
    @Autowired
    private  DriverService driverService;

    @GetMapping("drivers_info")
    public JsonData getDriversInfoList(){
        return JsonData.buildSuccess(driverService.getDriversInfoList());

    }
    @GetMapping("drivers_status")
    public JsonData getDriverCountByStatus(){

    return JsonData.buildSuccess(driverService.getDriversCountByStatus());
    }


    @PostMapping("add")
    public JsonData addDriver(@RequestBody Driver driver){
        Integer res = driverService.addDriver(driver);
        if(res==1){
            return JsonData.buildSuccess("");
        }
        else{
            return JsonData.buildError("身份证或驾驶证已重复，请检查!");
        }

    }

    @GetMapping("export")
    public void Export(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment; filename=drivers.xlsx");
        List<Driver> data = driverService.getDriversInfoList();
        EasyExcel.write(response.getOutputStream(), Driver.class).sheet("驾驶员信息").doWrite(data);
    }





}
